package ink.yuanchu.picachoframework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ziyuan
 * @Date: 2021/02/19/15:58
 * @Description:
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    /**
     * 配置swagger 文档信息和环境状态的开关闭规则
     * 仅在开发环境 和 测试环境打开
     * @param environment
     * @return
     */
    @Bean
    public Docket docket(Environment environment){
        Profiles of = Profiles.of("dev","test");
        boolean b = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b);
    }

    /**
     * 配置文档信息
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact("ziyuan", "http://www.yuanchu.ink/", "934905117@qq.com");
        return new ApiInfo(
                // 标题
                "Picacho Framework api 文档",
                // 描述
                "api文档",
                // 版本
                "v1.0",
                // 组织链接
                "www.yuanchu.ink",
                // 联系人信息
                contact,
                // 许可
                "Apach 2.0 许可",
                // 许可连接
                "null",
                // 扩展
                new ArrayList<>()
        );
    }
}
